<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Lost_usernames extends CI_Controller {
	
	function __construct()
	{
		parent::__construct();
	}
	
	/**
	 * Default Lost_usernames page
	 *
	 * @access	public
	 * @output	lost_usernames.tpl
	 */
	public function index()
	{		
		$data['title'] = "Lost your username?";
		if($this->session->flashdata('notice') != '')
			$data['notice'] = $this->session->flashdata('notice');
		$this->parser->parse("lost_usernames/lost_usernames.tpl", $data);
	}
	
	/**
	 * find
	 *
	 * @access	public
	 * @redirect	Success: 		/lost_usernames
	 *				Unsuccessful: 	/lost_usernames
	 */
	public function find()
	{
		$this->load->helper(array('form', 'url'));
		$this->load->library(array('form_validation','email'));
		$this->load->model(array('User_model'));

		$this->form_validation->set_rules('email', 'Email', 'required');
		
		if ($this->form_validation->run() == TRUE)
		{
			$user = $this->User_model->get_user_by_email($this->input->post('email'));
			
			if($user != NULL)
			{
				// --- email ---
				$this->email->from('support@achieveit.com', 'AchieveIt');
				$this->email->to($this->input->post('email')); 

				$this->email->subject('[AchieveIt] Lost username');
				$this->email->message($user->username); 
				$this->email->send();
				// --- end email ---
			}
		}
		
		$this->session->set_flashdata('notice', 'Your username has been emailed to you');
		
		redirect('/lost_usernames');
	}
}
?>